﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;
using System.Data;

public partial class client_Search_Workorder : System.Web.UI.Page
{
    String clientid="";
    protected void Page_Load(object sender, EventArgs e)
    {
         //clientid = Request.QueryString["client_id"];
        clientid=(string)Session["client_id"];
        DataAccess.i2iPortalTableAdapters.SitesTableAdapter STA = new DataAccess.i2iPortalTableAdapters.SitesTableAdapter();
        DataTable st = STA.GetDataByClientID(Int32.Parse(clientid));
        //DataTable st = STA.GetDataByClientID(Int32.Parse(clientid));

        foreach (DataRow site in st.Rows)
        {
            String siteName = site["siteName"].ToString();
            string siteid = site["site_id"].ToString();

            ListItem siteItem = new ListItem();
            siteItem.Text = siteName;
            siteItem.Value = siteid;
            Sites_DropDownList.Items.Add(siteItem);
        }
    }
    protected void searchbuttonClicked(object sender, EventArgs e)
    {
        string workorderno = workorderTextbox.Text;
        string startdate = startdate_textbox.Text;
        string enddate = enddate_textbox.Text;
        //string completed = statusCheckbox.Checked?"1":"0";
        string completed = status_dropdownlist.SelectedValue;
        if (workorderno.Equals(""))
        {
            if (Sites_DropDownList.SelectedIndex == 0)
            {
                if (startdate == "" || enddate == "")
                {
                    if (completed == "2")
                    {
                        CreateConn obj = new CreateConn();
                        string query = "select workorder_id, workorder_reference_no,date_submitted,machine_id,status"
                  + " FROM WorkOrders WHERE  machine_id in (SELECT machine_id FROM Machines WHERE site_id in (SELECT site_id FROM Sites WHERE client_id='" + Int32.Parse(clientid) + "'))";
                        DataTable dt = obj.SQLConnDataTable(query);
                        Session["searchresult"] = dt;
                    }
                    else
                    {
                        CreateConn obj = new CreateConn();
                        string query = "select workorder_id, workorder_reference_no,date_submitted,machine_id,status"
               + " FROM WorkOrders WHERE status='" + completed.ToString() + "' AND machine_id in (SELECT machine_id FROM Machines WHERE site_id in (SELECT site_id FROM Sites WHERE client_id='" + Int32.Parse(clientid) + "'))";
                        DataTable dt = obj.SQLConnDataTable(query);
                        Session["searchresult"] = dt;
                    }
                }
                else
                {
                    if(completed=="2"){
                        CreateConn obj = new CreateConn();
                        string query = "select workorder_id, workorder_reference_no,date_submitted,machine_id,status"
                    + " FROM WorkOrders WHERE  date_submitted BETWEEN convert(datetime,'" + startdate + "', 103)"
                    + "and convert(datetime,'" + enddate + "', 103) AND machine_id in (SELECT machine_id FROM Machines WHERE site_id in (SELECT site_id FROM Sites WHERE client_id='" + Int32.Parse(clientid) + "'))";
                        DataTable dt = obj.SQLConnDataTable(query);
                        Session["searchresult"] = dt;
                    }else{
                        CreateConn obj = new CreateConn();
                        string query = "select workorder_id, workorder_reference_no,date_submitted,machine_id,status"
                    + " FROM WorkOrders WHERE status='" + completed.ToString() + "' AND  date_submitted BETWEEN convert(datetime,'" + startdate + "', 103)"
                    + "and convert(datetime,'" + enddate + "', 103) AND machine_id in (SELECT machine_id FROM Machines WHERE site_id in (SELECT site_id FROM Sites WHERE client_id='" + Int32.Parse(clientid) + "'))";
                        DataTable dt = obj.SQLConnDataTable(query);
                        Session["searchresult"] = dt;
                    }
                 
                }
            }
            else
            {
                if (startdate == "" || enddate == "")
                {
                    if (completed == "2")
                    {
                        int selectedsite = Int32.Parse(Sites_DropDownList.SelectedValue);
                        CreateConn obj = new CreateConn();
                        string query = "select workorder_id, workorder_reference_no,date_submitted,machine_id,status"
                            + " FROM WorkOrders WHERE machine_id in (SELECT machine_id FROM Machines WHERE site_id='" + selectedsite + "')";
                        DataTable dt = obj.SQLConnDataTable(query);
                        Session["searchresult"] = dt;
                    }
                    else
                    {
                        int selectedsite = Int32.Parse(Sites_DropDownList.SelectedValue);
                        CreateConn obj = new CreateConn();
                        string query = "select workorder_id, workorder_reference_no,date_submitted,machine_id,status"
                            + " FROM WorkOrders WHERE status='" + completed.ToString() + "' AND machine_id in (SELECT machine_id FROM Machines WHERE site_id='" + selectedsite + "')";
                        DataTable dt = obj.SQLConnDataTable(query);
                        Session["searchresult"] = dt;
                    }
                }
                else
                {
                    if (completed == "2")
                    {
                        int selectedsite = Int32.Parse(Sites_DropDownList.SelectedValue);
                        CreateConn obj = new CreateConn();
                        string query = "select workorder_id, workorder_reference_no,date_submitted,machine_id,status"
                            + " FROM WorkOrders WHERE  date_submitted BETWEEN convert(datetime,'" + startdate + "', 103)"
                            + "and convert(datetime,'" + enddate + "', 103) AND machine_id in (SELECT machine_id FROM Machines WHERE site_id='" + selectedsite + "')";
                        DataTable dt = obj.SQLConnDataTable(query);
                        Session["searchresult"] = dt;
                    }
                    else
                    {
                        int selectedsite = Int32.Parse(Sites_DropDownList.SelectedValue);
                        CreateConn obj = new CreateConn();
                        string query = "select workorder_id, workorder_reference_no,date_submitted,machine_id,status"
                            + " FROM WorkOrders WHERE status='" + completed.ToString() + "' AND  date_submitted BETWEEN convert(datetime,'" + startdate + "', 103)"
                            + "and convert(datetime,'" + enddate + "', 103) AND machine_id in (SELECT machine_id FROM Machines WHERE site_id='" + selectedsite + "')";
                        DataTable dt = obj.SQLConnDataTable(query);
                        Session["searchresult"] = dt;
                    }
                }
     
            }
        }
        else
        {
            CreateConn obj = new CreateConn();
            string query = "select workorder_id, workorder_reference_no,date_submitted,machine_id,status"
                + " FROM WorkOrders WHERE status='" + completed.ToString() + "' AND workorder_reference_no='"
                + workorderno + "'";
            DataTable dt = obj.SQLConnDataTable(query);
            Session["searchresult"] = dt;
        }

        Response.Redirect("SearchResults.aspx");
    }
    protected void backbuttonclicked(object sender, EventArgs e)
    {
        Server.Transfer("welcome.aspx");
    }
}
